<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>机器人列表管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="models-search">
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit">新增机器人</el-button>
      </el-form-item>
    </el-form>
    <!-- 标签选择器 -->

    <el-tag
      :key="index"
      v-for="(item, index) in TagClickList"
      closable
      :disable-transitions="false"
      @close="handleClose(item)">
      {{item.modelTag}}
    </el-tag>

    <!--列表-->
    <el-table  :data="listData" highlight-current-row v-loading="loading"  element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable align="center" prop="id" label="主键id" width="300" v-if="false">
      </el-table-column>
      <el-table-column sortable align="center" prop="promptWord" label="提示词" width="300" v-if="false">
      </el-table-column>
      <el-table-column sortable align="center" prop="docIds" label="文档" width="300" v-if="false">
      </el-table-column>
      <el-table-column sortable align="center" prop="manualReplyContent" label="人工回复文案" width="300" v-if="false">
      </el-table-column>
      <el-table-column sortable align="center" prop="welcomeMessage" label="欢迎语" width="300" v-if="false">
      </el-table-column>
      <el-table-column sortable align="center" prop="rebotName" label="机器人名称" width="200">
      </el-table-column>
      <el-table-column sortable align="center" prop="deploy" label="是否部署" width="100">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.deploy ? 'green' : 'red' }">
            {{ scope.row.deploy  ? '已部署' : '未部署' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" prop="config" label="是否配置" width="100">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.config ? 'green' : 'red' }">
            {{ scope.row.config  ? '已配置' : '未配置' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" prop="createTime" label="创建时间" width="200">
      </el-table-column>
      <el-table-column align="center" label="操作" max-width="400">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" @click="handleDeploy(scope.$index, scope.row)">部署详情</el-button>
          <el-button size="mini" @click="handleApply(scope.$index, scope.row)">机器人体验</el-button>

        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 部署详情界面 -->
    <el-dialog :title="title" :visible.sync="deplayFormVisible" width="30%" @click='closeDialog("deplay")'>
      <el-form label-width="120px" :model="deplayForm" ref="editForm" :rules="rules">
        <el-form-item label="主键" prop="id" v-show="false">
          <el-input readonly size="small" v-model="deplayForm.id" auto-complete="off" placeholder="请输入主键"></el-input>
        </el-form-item>
        <el-form-item label="url" prop="url">
          <el-input size="small" v-model="deplayForm.url" auto-complete="off" ></el-input>
        </el-form-item>
        <el-form-item label="请求方式" prop="method">
          <el-input size="small" v-model="deplayForm.method" auto-complete="off" ></el-input>
        </el-form-item>
        <el-form-item label="请求参数" prop="params">
          <el-input size="small" v-model="deplayForm.params" auto-complete="off" ></el-input>
        </el-form-item>
        <el-form-item label="请求token" prop="token">
          <el-input size="small" v-model="deplayForm.token" auto-complete="off" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('deplayForm')">保存</el-button>-->
        <el-button size="small" @click='closeDialog("deplay")'>退出</el-button>

      </div>
    </el-dialog>

  </div>
</template>
<script>
import Pagination from "../../components/Pagination.vue";
import {
  rebotDeployDetail,
  rebotList
} from "../../api/userMG";
export default {
  data() {
    return {
      loading: false, //是显示加载
      deplayFormVisible: false, //控制部署详情页面显示与隐藏
      title: '部署详情',
      imageUrl: '',
      deplayForm: {
        id: '',
        method:'',
        url:'',
        params:'',
        token:'',
      },
      editForm: {
        id: '',
        rebotName:'',
        promptWord:'',
        manualReplyContent:'',
        welcomeMessage:'',
        docIds:'',
      },
      // rules 表单验证
      rules: {
        categoryName: [
          { required: true, message: '请输入类目名称', trigger: 'blur' }
        ],
      },
      formInline: {
        pageNum: 1,
        pageSize: 10,
        ipname:''
      },

      listData: [], //模型
      TagList:[],//所有tag列表
      TagClickList:new Set(),//选择的tag列表
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 1
      }
    }
  },
  // 注册组件
  components: {
    Pagination,
  },
  /**
   * 数据发生改变
   */

  watch: {
  },
  /**
   * 创建完毕
   */

  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取类目列表
    getdata(parameter) {
      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */

      rebotList(parameter)
        .then(res => {
          this.loading = false
          console.log(res)
          if(res.code==null){
            this.$router.push({ name: 'login' });//跳转到查询界面
          }
          if (res.code!=0) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.loading = false
            this.listData = res.data.records
            // 分页赋值
            this.pageparm.currentPage = this.formInline.current
            this.pageparm.pageSize = this.formInline.size
            this.pageparm.total = res.data.total
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('获取列表失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      console.log(parm)
      this.formInline.pageNum = parm.currentPage
      this.formInline.pageSize = parm.pageSize
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      // this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.editForm.title = '修改机器人'
        this.editForm.id=row.id
        this.editForm.rebotName = row.rebotName
        this.editForm.promptWord = row.promptWord
        this.editForm.docIds = row.docIds
        this.editForm.manualReplyContent = row.manualReplyContent
        this.editForm.welcomeMessage = row.welcomeMessage
        // this.editForm.imageurl = row.imageurl
        console.log(this.editForm)
        this.$router.push({ name: 'rebotedit', params:this.editForm});//跳转到添加编辑界面
      } else {
        this.$router.push({ name: 'rebotedit', params:this.editForm });//跳转到添加编辑界面
      }
    },
    //显示编辑界面
    handleApply: function(index, row) {
      // this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.editForm.title = '机器人体验'
        this.editForm.id=row.id
        this.editForm.rebotName = row.rebotName
        this.editForm.promptWord = row.promptWord
        this.editForm.docIds = row.docIds
        // this.editForm.imageurl = row.imageurl
        console.log(this.editForm)
        this.$router.push({ name: 'rebotedit', params:this.editForm});//跳转到添加编辑界面
      }
    },
    //显示部署详情界面
    handleDeploy(index, row) {
      if (row != undefined && row != 'undefined') {
        rebotDeployDetail(row)
          .then(res => {
            this.loading = false
            console.log(res)
            if(res.code==null){
              this.$router.push({ name: 'login' });//跳转到查询界面
            }
            if (res.code!=0) {
              this.$message({
                type: 'info',
                message: res.msg
              })
            } else {
              this.loading = false
              this.deplayForm = res.data
              // 分页赋值
              this.pageparm.currentPage = this.formInline.current
              this.pageparm.pageSize = this.formInline.size
              this.pageparm.total = res.data.total
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('获取列表失败，请稍后再试！')
          })
        this.deplayFormVisible = true
      }
    },
    // 选中菜单
    changemenu(arr) {
      let change = []
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
          change.push(arr[i].id)
        }
      }
      this.checkmenu = change
    },

    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'deplay') {
        this.deplayFormVisible = false
      }
    },


  }

}
</script>
<style scoped>
.models-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>
